package org.jeecg.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.online.api.IOnlineBaseExtAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.online.cgform.service.IOnlCgformHeadService;
import org.jeecg.modules.online.cgform.util.CgformUtil;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportAPIService;
import org.jeecg.modules.online.config.exception.DBException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表单设计器Online翻译实现类【desform】
 */
@Slf4j
@Component("onlineBaseExtAPI")
public class OnlineBaseExtAPI implements IOnlineBaseExtAPI {

    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    IOnlCgreportAPIService onlCgreportAPIService;

    @Override
    public String cgformPostCrazyForm(String tableName, JSONObject jsonObject) throws Exception {
        String uuid = CgformUtil.randomId();
        jsonObject.put("id", uuid);
        this.onlCgformHeadService.addCrazyFormData(tableName, jsonObject);
        return uuid;
    }

    @Override
    public String cgformPutCrazyForm(String tableName, JSONObject jsonObject) throws Exception {
        //update-begin-author:lvdandan date:20201023 for:LOWCOD-747 online表单开发集成表单设计器编辑时 去掉标准字段
        jsonObject.remove("create_by");
        jsonObject.remove("create_time");
        jsonObject.remove("update_by");
        jsonObject.remove("update_time");
        //update-end-author:lvdandan date:20201023 for:LOWCOD-747 online表单开发集成表单设计器编辑时 去掉标准字段
        this.onlCgformHeadService.editCrazyFormData(tableName, jsonObject);
        return jsonObject.getString("id");
    }

    @Override
    public String cgformDeleteDataByCode(String cgformCode, String dataIds) {
        return onlCgformHeadService.deleteDataByCode(cgformCode, dataIds);
    }

    @Override
    public JSONObject cgformQueryAllDataByTableName(String tableName, String dataIds) {
        try {
            return onlCgformHeadService.queryAllDataByTableNameForDesform(tableName, dataIds);
        } catch (DBException e) {
            log.error("查询主子表数据失败：", e);
            return null;
        }
    }

    @Override
    public Map<String, Object> cgreportGetData(String code, String forceKey, String dataList) {
        Map<String, Object> params = new HashMap<>();
        params.put(forceKey, dataList);
        params.put("getAll", true);
        return onlCgreportAPIService.getDataByCode(code, params);
    }

    @Override
    public List<DictModel> cgreportGetDataPackage(String code, String dictText, String dictCode, String dataList) {
        String forceKey = "force_" + dictCode;
        Map<String, Object> result = this.cgreportGetData(code, forceKey, dataList);
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> records = (List<Map<String, Object>>) result.get("records");
        List<DictModel> dictModelList = new ArrayList<>();
        for (Map<String, Object> record : records) {
            String value = (String) record.get(dictCode);
            String text = (String) record.get(dictText);
            dictModelList.add(new DictModel(value, text));
        }
        return dictModelList;
    }

}
